package com.salinity.kun.task;

import java.io.IOException;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.List;

import com.salinity.kun.algorithm.harmonic.TideSymbol;
import com.salinity.kun.helper.BaseHydrologyDataHelper;
import com.salinity.kun.helper.ConstantHelper;
import com.salinity.kun.model.StationDateData;
import com.salinity.kun.util.DateUtil;

public class Baimao implements ITask {

	@Override
	public boolean run() throws IOException, ParseException {
		List<TideSymbol> tideSymbolList = new ArrayList<>();
		tideSymbolList = new ArrayList<>();
		tideSymbolList.add(TideSymbol.M2);
		tideSymbolList.add(TideSymbol.S2);
//		tideSymbolList.add(TideSymbol.N2);
//		 tideSymbolList.add(TideSymbol.K2);

		tideSymbolList.add(TideSymbol.K1);
		tideSymbolList.add(TideSymbol.O1);
//		 tideSymbolList.add(TideSymbol.P1);
//		tideSymbolList.add(TideSymbol.Q1);

		tideSymbolList.add(TideSymbol.M4);
		tideSymbolList.add(TideSymbol.M6);
		tideSymbolList.add(TideSymbol.MS4);
		Path inputPath = Paths.get("E:\\Kuner\\BYLW\\Jworkspace\\saltyCode\\testify\\Baimao_linear.csv");

		Path interpolationPath = Paths.get("E:\\Kuner\\BYLW\\Jworkspace\\saltyCode\\testify\\Baimao_linear.csv");
		// Path outputRawDataPath =
		// Paths.get("E:\\Kuner\\BYLW\\Jworkspace\\saltyCode\\testify\\Baimao_18_4.csv");

		Path outputPath = Paths.get("E:\\Kuner\\BYLW\\Jworkspace\\saltyCode\\testify\\mid\\bm\\");
		SimpleDateFormat sdf = ConstantHelper.getDateFormat();

		Date endDate = null;
		int hours = 130;
		int dateColumn = 1;
		int dataRow = 1;
		int dataColumn = 2;
		int divNum = 1;

		Date startDate;
		int state = 1; // 0 潮型选择， 1 积分起点选择

		int index;
//
//		String[] startArr = { "2017/12/21 1:30",  "2017/12/5 0:10","2017/11/30 8:50", "2017/9/15 6:40",
//				"2017/10/30 8:10", "2017/7/19 7:40", "2017/7/14 1:55", "2017/7/5 9:40" };

//		String[] startArr = { "2017/12/21 1:30",  "2017/12/5 0:10","2017/11/30 8:50", 
//				"2017/10/30 8:10", "2017/7/19 7:40", "2017/7/14 1:55", "2017/7/5 9:40" };
		
		String[] startArr = { "2017/12/21 1:20",  "2017/12/5 0:20","2017/11/30 8:50", "2017/9/15 6:40",
				"2017/10/30 8:30", "2017/7/19 7:40", "2017/7/14 1:30", "2017/7/5 9:40" };
	//	String[] startArr = {  "2017/12/5 0:20"};
		// int index = 10;
		HarmonicAnalysisTask HATask = new HarmonicAnalysisTask();
		Calendar calendar;

		for (int i = 0; i < startArr.length; i++) {
			if (state == 1) {
				index = 1;
			} else {
				index = 10;
			}
			startDate = sdf.parse(startArr[i]);
			calendar = DateUtil.Date2Calendar(startDate);
			while (index > 0) {
				endDate = DateUtil.LDTime2Date(DateUtil.Date2LDTime(calendar.getTime()).plusHours(hours + 1));
				List<StationDateData> dataList = BaseHydrologyDataHelper.getTidesFromFile(inputPath, calendar.getTime(), 5,
						hours, dateColumn, dataRow, dataColumn, divNum);
				// BaseHydrologyDataHelper.writeToFile(dataList, outputRawDataPath);

				// List<StationDateData> linearList =
				// BaseHydrologyDataHelper.doLinearInterpolation(dataList,
				// calendar.getTime(), endDate, 5);
				// BaseHydrologyDataHelper.writeToFile(dataList, interpolationPath);
				HATask.doCalculation(calendar.getTime(), endDate, 5, 5, hours, tideSymbolList, dataList, state,
						outputPath.resolve("baimao"));

				if (state == 1) {
					calendar.add(Calendar.MINUTE, 10);
				} else {
					calendar.add(Calendar.DAY_OF_MONTH, 1);
				}
				index--;
			}
		}

		return false;
	}

	public static void main(String[] args) {
		ITask task = new Baimao();
		try {
			task.run();
		} catch (IOException | ParseException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
}
